﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Gmantis.Controls.Chart
{
    public class Star : RPolygon
    {
        // Fields
        public static readonly DependencyProperty InnerRadiusProperty = Utils.RegisterProperty("InnerRadius", typeof(double), typeof(Star), new PropertyChangedCallback(Star.OnInnerRadiusChanged), 0.5);

        // Methods
        public Star()
        {
            this.InnerRadius = 0.5;
            base.NumVertices = 4;
        }

        internal override object Clone()
        {
            Star clone = new Star
            {
                NumVertices = base.NumVertices,
                InnerRadius = this.InnerRadius
            };
            base.CloneAttributes(clone);
            return clone;
        }

        private static void OnInnerRadiusChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            Star star = (Star)obj;
            star.UpdateGeometry(null, star.Size);
        }

        protected override void UpdateGeometry(PathGeometry pg, Size sz)
        {
            if (pg == null)
            {
                pg = (PathGeometry)base.geometry;
            }
            if (sz.IsEmpty)
            {
                sz = this.Size;
            }
            pg.Figures.Clear();
            double rx = 0.5 * sz.Width;
            double ry = 0.5 * sz.Height;
            double num3 = 0.5 * base.StrokeThickness;
            double innerRadius = this.InnerRadius;
            if ((innerRadius > 1.0) || (innerRadius < 0.0))
            {
                innerRadius = 0.5;
            }
            double num5 = innerRadius * rx;
            double num6 = innerRadius * ry;
            Point center = new Point(rx + num3, ry + num3);
            double numVertices = base.NumVertices;
            PathFigure figure = new PathFigure
            {
                StartPoint = new Point(center.X + rx, center.Y),
                IsFilled = true,
                IsClosed = true
            };
            for (int i = 1; i <= numVertices; i++)
            {
                double d = (((i - 0.5) / numVertices) * 2.0) * 3.1415926535897931;
                double x = center.X + (num5 * Math.Cos(d));
                double y = center.Y + (num6 * Math.Sin(d));
                LineSegment segment = new LineSegment
                {
                    Point = new Point(x, y)
                };
                figure.Segments.Add(segment);
                if (i == numVertices)
                {
                    break;
                }
                d = ((((double)i) / numVertices) * 2.0) * 3.1415926535897931;
                x = center.X + (rx * Math.Cos(d));
                y = center.Y + (ry * Math.Sin(d));
                LineSegment segment2 = new LineSegment
                {
                    Point = new Point(x, y)
                };
                figure.Segments.Add(segment2);
            }
            base.AddFakeEllipse(pg, center, rx, ry, num3);
            pg.Figures.Add(figure);
            Canvas.SetLeft(this, (this.symCenter.X - rx) - num3);
            Canvas.SetTop(this, (this.symCenter.Y - ry) - num3);
        }

        // Properties
        public double InnerRadius
        {
            get
            {
                return (double)base.GetValue(InnerRadiusProperty);
            }
            set
            {
                base.SetValue(InnerRadiusProperty, value);
            }
        }

        public override Size Size
        {
            get
            {
                return base.Size;
            }
            set
            {
                base.Size = value;
                this.UpdateGeometry(null, Size.Empty);
            }
        }
    }
}
